wayland: Implement gdk_utf8_to_string_target
authorCarlos Garnacho <carlosg@gnome.org>
Mon, 27 Jun 2016 09:57:21 +0000 (11:57 +0200)
committerCarlos Garnacho <carlosg@gnome.org>
Thu, 30 Jun 2016 12:10:26 +0000 (14:10 +0200)
The sanitize_utf8() function has been copied from X11 so both
backends behave the same. This allows interaction with older clients
(mainly through Xwayland, and the STRING selection target) that
request non-utf8 text.

https://bugzilla.gnome.org/show_bug.cgi?id=768082

gdk/wayland/gdkselection-wayland.c

index 69a2145e045217e097758f2d31d64a44677fb3d9..c21cafd4a0acfc321c03795eff5e9c0e4d5e3162 100644 (file)
@@ -1385,11 +1385,69 @@ _gdk_wayland_display_text_property_to_utf8_list (GdkDisplay    *display,
   return nitems;
 }
 
+/* This function has been copied straight from the x11 backend */
+static gchar *
+sanitize_utf8 (const gchar *src,
+               gboolean return_latin1)
+{
+  gint len = strlen (src);
+  GString *result = g_string_sized_new (len);
+  const gchar *p = src;
+
+  while (*p)
+    {
+      if (*p == '\r')
+        {
+          p++;
+          if (*p == '\n')
+            p++;
+
+          g_string_append_c (result, '\n');
+        }
+      else
+        {
+          gunichar ch = g_utf8_get_char (p);
+
+          if (!((ch < 0x20 && ch != '\t' && ch != '\n') || (ch >= 0x7f && ch < 0xa0)))
+            {
+              if (return_latin1)
+                {
+                  if (ch <= 0xff)
+                    g_string_append_c (result, ch);
+                  else
+                    g_string_append_printf (result,
+                                            ch < 0x10000 ? "\\u%04x" : "\\U%08x",
+                                            ch);
+                }
+              else
+                {
+                  char buf[7];
+                  gint buflen;
+
+                  buflen = g_unichar_to_utf8 (ch, buf);
+                  g_string_append_len (result, buf, buflen);
+                }
+            }
+
+          p = g_utf8_next_char (p);
+        }
+    }
+
+  return g_string_free (result, FALSE);
+}
+
 gchar *
 _gdk_wayland_display_utf8_to_string_target (GdkDisplay  *display,
                                             const gchar *str)
 {
-  return NULL;
+  /* This is mainly needed when interfacing with old clients through
+   * Xwayland, the STRING target could be used, and passed as-is
+   * by the compositor.
+   *
+   * There's already some handling of this atom (aka "mimetype" in
+   * this backend) in common code, so we end up in this vfunc.
+   */
+  return sanitize_utf8 (str, TRUE);
 }
 
 void